'The Program Entry Point
' $HeadURL: http://gabbscript.googlecode.com/svn/trunk/Gabbscript/Main.vb $
' $Author: iwannalightsaber@hotmail.com $
' $Revision: 106 $
' $Date: 2007-11-20 19:40:44 +0000 (Tue, 20 Nov 2007) $

Imports Gabbscript.Parser

Module Main

    Sub Main(ByVal args() As String)
        Dim fileName As String = ""

        'Initialize script
        Dim s As Script = New Script()

        'Register Module Functions
        AndrewFunctions.RegisterFunctions(s.funcList)
        BrendanFunctions.RegisterFunctions(s.funcList)
        BrookFunctions.RegisterFunctions(s.funcList)

        'Check the amount of arguments
        If args.Length = 0 Then

            Console.WriteLine("Enter a filename to load, or type console! :)")
            'Ask the user for a file to load
            fileName = Console.ReadLine()
            Console.WriteLine()
        Else
            'Filename is first program parameter
            fileName = args(0)
        End If

        If fileName.ToLower() = "console" Then
            Console.WriteLine("Entering console mode :P")
            Console.WriteLine("Type gabbEnd to finish parsing")

            Dim line As String = Console.ReadLine()
            Dim lines As ArrayList = New ArrayList()

            Do While line <> "gabbEnd"
                lines.Add(line)
                line = Console.ReadLine()
            Loop

            Console.WriteLine()

            s.SetLines(lines)
        Else
            'Load the file
            Try
                s.LoadFile(fileName)
            Catch ex As System.IO.FileNotFoundException
                Console.WriteLine("Unable to find file, exiting program!")
                Exit Sub
            End Try
        End If

        

        'Parse the file
        s.ParseFile()

        'Run the code :)
        s.Run()
    End Sub

End Module
